package com.movie.demo.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movie.demo.mapper.SeatMapper;
import com.movie.demo.pojo.Seat;
import com.movie.demo.pojo.vo.SeatInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Transactional
public class SeatService extends ServiceImpl<SeatMapper, Seat> implements com.movie.demo.service.SeatService{
    @Autowired
    private SeatMapper seatMapper;
    @Override
    public List<Seat> selectSeat(int mid,int rid, int sid,int cid) {
        List<Seat> seatList=selectList(new EntityWrapper<Seat>().eq("mid",mid).and().eq("room_id",rid)
                .and().eq("session_id",sid).and().eq("cinema_id",cid));
        return  seatList;
    }

    @Override
    public boolean addSeat(Seat seat) {
        Seat seat1=this.selectOne(new EntityWrapper<Seat>().eq("mid",seat.getMid()).and().eq("session_id",seat.getSession_id()).
                and().eq("room_id",seat.getRoom_id()).and()
                .eq("cinema_id",seat.getCinema_id()).and().eq("seat",seat.getSeat()));
        if (seat1==null){
            boolean flag=this.insert(seat);
            return flag;
        }
        seat.setId(seat1.getId());
        return true;
    }

    @Override
    public List<Seat> allSeat() {
        List<Seat> seatList=selectList(new EntityWrapper<Seat>());
        return  seatList;
    }

    @Override
    public boolean updateSeatState(int id ,int flag) {
        Seat seat = this.selectById(id);
        if (Objects.isNull(seat) || seat == null)return false;
        seat.setFlag(flag);
        boolean result=this.updateById(seat);
        return result;
    }

    @Override
    public boolean DeleteSeat(int id) {
        boolean result=this.deleteById(id);
        return result;
    }

    @Override
    public List<SeatInfo> selectSeatInfo() {
        List<SeatInfo> seatInfoList=seatMapper.selectSeatInfo();
        return seatInfoList;
    }

    @Override
    public boolean selectSeatInfoOne(int mid, int session_id, int room_id, int cinema_id, String seat, int uid) {
        Seat seats=this.selectOne(new EntityWrapper<Seat>().eq("mid",mid).and().eq("session_id",session_id).and().eq("room_id",room_id).and()
        .eq("cinema_id",cinema_id).and().eq("seat",seat));
        if (seats!=null) {
            int uids =seats.getUid();
            if (uids==uid){
                this.deleteById(seats.getId());
                return  false;
            }
            return true;
        }
        else {
            Seat seat1 =new Seat();
            seat1.setMid(mid);
            seat1.setSession_id(session_id);
            seat1.setRoom_id(room_id);
            seat1.setCinema_id(cinema_id);
            seat1.setSeat(seat);
            seat1.setFlag(2);
            seat1.setUid(uid);
            this.insert(seat1);
        }
        return  false;
    }
}
